using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace sforceExplorer
{
	/// <summary>
	/// Summary description for frmPasswordEntry.
	/// </summary>
	public class frmPasswordEntry : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.TextBox txtPasswordConfirm;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnSet;
		private System.Windows.Forms.Button btnCancel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private string _userID = "";
		private string _userName = "";

		public frmPasswordEntry(string userID, string userName)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			_userID = userID;
			_userName = userName;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.txtPassword = new System.Windows.Forms.TextBox();
			this.txtPasswordConfirm = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.btnSet = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(112, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Enter new password:";
			// 
			// txtPassword
			// 
			this.txtPassword.Location = new System.Drawing.Point(16, 32);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '*';
			this.txtPassword.Size = new System.Drawing.Size(232, 20);
			this.txtPassword.TabIndex = 1;
			this.txtPassword.Text = "";
			// 
			// txtPasswordConfirm
			// 
			this.txtPasswordConfirm.Location = new System.Drawing.Point(16, 80);
			this.txtPasswordConfirm.Name = "txtPasswordConfirm";
			this.txtPasswordConfirm.PasswordChar = '*';
			this.txtPasswordConfirm.Size = new System.Drawing.Size(232, 20);
			this.txtPasswordConfirm.TabIndex = 1;
			this.txtPasswordConfirm.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(112, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "Confirm new password:";
			// 
			// btnSet
			// 
			this.btnSet.Location = new System.Drawing.Point(272, 8);
			this.btnSet.Name = "btnSet";
			this.btnSet.Size = new System.Drawing.Size(96, 24);
			this.btnSet.TabIndex = 2;
			this.btnSet.Text = "Set Password";
			this.btnSet.Click += new System.EventHandler(this.btnSet_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(272, 40);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(96, 24);
			this.btnCancel.TabIndex = 2;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// frmPasswordEntry
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(376, 126);
			this.Controls.Add(this.btnSet);
			this.Controls.Add(this.txtPassword);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.txtPasswordConfirm);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.btnCancel);
			this.Name = "frmPasswordEntry";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Enter Password Dialog";
			this.ResumeLayout(false);

		}
		#endregion

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		}

		private void btnSet_Click(object sender, System.EventArgs e)
		{
			try 
			{
				if (this.txtPassword.Text == this.txtPasswordConfirm.Text) 
				{
					if (System.Windows.Forms.MessageBox.Show("Please confirm that you want to set the password for " + _userName + ".", "Partner Sample", System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Question).Equals(System.Windows.Forms.DialogResult.OK)) 
					{
						sforce.SetPasswordResult spr = Utility.binding.setPassword(_userID, this.txtPassword.Text);
						System.Windows.Forms.MessageBox.Show("The password for " + _userName + " has bee reset.", "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
						this.DialogResult = System.Windows.Forms.DialogResult.OK;
						this.Close();
					}
				} 
				else 
				{
					System.Windows.Forms.MessageBox.Show("The passwords don't match, please re-enter.", "Patner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
					this.txtPasswordConfirm.Text = "";
					this.txtPassword.Text = "";
				}
			} 
			catch (Exception ex) 
			{
				System.Windows.Forms.MessageBox.Show("Password was not set: " + ex.Message, "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
			}
		}
	}
}
